import 'package:dmz/utils/Utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../routers/routers.dart';
import '../../../utils/ColorUtils.dart';
import '../controller/set_infor_controller.dart';
import '../model/moto_version.dart';

class ForceDialog extends StatefulWidget {
  final MotoVersion model;
  const ForceDialog({super.key, required this.model});

  @override
  State<StatefulWidget> createState() {
    return _ForceDialog();
  }
}

class _ForceDialog extends State<ForceDialog> {
  String? title;
  String? cardName;
  String? typeName = '';
  String? cardDescription = '升级内容：';
  String? cardSize = "大小：";
  int? fileSize = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    title = '检测到你的车辆固件有新的版本';
    if (Utils.isNotEmptyString(widget.model.ebike_name)) {
      cardName = '车辆名称：${widget.model.ebike_name!}';
    } else {
      if (Utils.isNotEmptyString(widget.model.ebike_type_name)) {
        cardName = '车辆名称：${widget.model.ebike_type_name}';
      } else {
        cardName = '车辆名称：';
      }
    }
    if (Utils.isNotEmptyList(widget.model.details)) {
      for (int i = 0; i < widget.model.details!.length; i++) {
        fileSize = fileSize! + widget.model.details![i].file_size!;
        if (i < widget.model.details!.length - 1) {
          if (Utils.isNotEmptyString(widget.model.details![i].description)) {
            cardDescription =
                '${cardDescription!}${widget.model.details![i].description!},';
          }
        } else {
          if (Utils.isNotEmptyString(widget.model.details![i].description)) {
            cardDescription =
                '${cardDescription!}${widget.model.details![i].description!}';
          }
        }
        if (Utils.isNotEmptyList(widget.model.details![i].type)) {
          for (int j = 0; j < widget.model.details![i].type!.length; j++) {
            if (j < widget.model.details![i].type!.length - 1) {
              if (Utils.isNotEmptyString(
                  widget.model.details![i].type![j].name)) {
                typeName =
                    '${typeName!}${widget.model.details![i].type![j].name!},';
              }
            } else {
              if (Utils.isNotEmptyString(
                  widget.model.details![i].type![j].name)) {
                typeName =
                    '${typeName!}${widget.model.details![i].type![j].name!}';
              }
            }
          }
        }
      }
    }

    double tempSize = (fileSize! / 1024) / 1024;
    cardSize = '${cardSize!}${tempSize.toStringAsFixed(5)}MB';

    print("----------------------------来到了强制升级页面");
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        // 拦截返回按钮
        onWillPop: () async {
          return true;
        },
        child: ConstrainedBox(
          constraints: BoxConstraints.expand(),
          child: GestureDetector(
            onTap: () {},
            child: Material(
              color: ColorUtils.color_transparent,
              child: GestureDetector(
                onTap: () {
                  // 在非强更模式下，防止误触弹框内容导致关闭弹框
                },
                child: Center(
                  child: Container(
                    margin: const EdgeInsets.symmetric(horizontal: 10),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Container(
                          padding: const EdgeInsets.symmetric(horizontal: 15),
                          decoration: const ShapeDecoration(
                              color: ColorUtils.color_white,
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.only(
                                      topLeft: Radius.circular(15.0),
                                      topRight: Radius.circular(15.0),
                                      bottomLeft: Radius.circular(15.0),
                                      bottomRight: Radius.circular(15.0)))),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              const SizedBox(
                                height: 20,
                              ),
                              Text(
                                title!,
                                style: const TextStyle(
                                    fontSize: 16,
                                    color: ColorUtils.color_black),
                              ),
                              Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(cardName!,
                                      style: const TextStyle(
                                          fontSize: 14,
                                          color: ColorUtils.color_black)),
                                  Text(typeName!,
                                      style: const TextStyle(
                                          fontSize: 14,
                                          color: ColorUtils.color_black)),
                                  Text(cardDescription!,
                                      style: const TextStyle(
                                          fontSize: 14,
                                          color: ColorUtils.color_black)),
                                  Text(cardSize!,
                                      style: const TextStyle(
                                          fontSize: 14,
                                          color: ColorUtils.color_black)),
                                  const SizedBox(
                                    height: 10,
                                  ),
                                  Row(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Container(
                                        alignment: Alignment.center,
                                        width: 200,
                                        height: 40,
                                        decoration: const BoxDecoration(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(30)),
                                          gradient: LinearGradient(
                                            begin:
                                                Alignment.topCenter, // 渐变起始位置
                                            end: Alignment
                                                .bottomCenter, // 渐变结束位置
                                            colors: [
                                              ColorUtils.color_6dd400,
                                              ColorUtils.color_20c468
                                            ], // 渐变色数组
                                          ),
                                        ),
                                        child: GestureDetector(
                                          onTap: () {
                                            Navigator.pop(context);
                                            Get.toNamed(RouteGet.setMotoInfor)
                                                ?.then((value) {
                                              Get.delete<
                                                  SetMotoInforController>();
                                            });
                                          },
                                          child: const Text(
                                            '立即升级',
                                            style: TextStyle(
                                                fontSize: 14,
                                                color: ColorUtils.color_white),
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                ],
                              ),
                              const SizedBox(
                                height: 20,
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        ));
  }
}
